package com.chaos

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite

import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.rememberImagePainter
import com.chaos.ui.theme.ChaosjetpackTheme
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ChaosjetpackTheme {
                Surface(color = MaterialTheme.colors.background) {
                    Content()
                }
            }
        }
    }
}

@Composable
fun TopBar() {

    TopAppBar(
        title = {
            Text(text = "首页")
        },
        actions = {
            IconButton(onClick = { doClick() }) {
                Icon(Icons.Filled.Favorite, contentDescription = null)
            }
        },
        contentColor = Color.Blue
    )

}

fun doClick() {
    print("onClick")
}

@Composable
fun Body(modifier: Modifier = Modifier) {
    val scrollState = rememberLazyListState()
    val coroutineScope = rememberCoroutineScope()
    val listSize = 100

    LazyColumn(state = scrollState) {
        items(listSize) {
            ImageListItem(it)
        }
    }


}

@Composable
fun ImageListItem(index: Int) {
    Row(verticalAlignment = Alignment.CenterVertically) {

        Image(
            painter = rememberImagePainter(
                data = "https://developer.android.com/images/brand/Android_Robot.png"
            ),
            contentDescription = "Android Logo",
            modifier = Modifier.size(50.dp)
        )
        Spacer(Modifier.width(10.dp))
        Text("Item #$index", style = MaterialTheme.typography.subtitle1)
    }
}

@Composable
fun Content() {
    Scaffold(topBar = { TopBar() }) { innerPadding ->
        Body(Modifier.padding(innerPadding))
    }
}

@Preview
@Composable
fun ContentPreview() {
    ChaosjetpackTheme {
        Content()
    }
}

//class Message() {
//    var id: String? = null
//    var username: String? = null
//    var shareCode: String? = null
//    var shareAddress: String? = null
//}
//
//@Composable
//fun Messages(messages: List<Message>) {
//    LazyColumn {
//        items(messages) { message ->
//            MessageCard(message)
//        }
//    }
//}
//
//@Composable
//fun MessageCard(message: Message) {
//    message.username?.let { Text(text = it) }
//    message.shareCode?.let { Text(text = it) }
//}